<?php


function appendFiles(&$files, $elements, $child, $baseDir)
{
	if ($elements)
	{
		foreach ($elements as $group)
		{
			$folder = isset($group['folder']) ? $group['folder'] . '/' : '';
			if (isset($group->$child))
			{
				foreach ($group->$child as $file)
					$files[] = "$baseDir$folder$file";
			}
		}
	}
}

function buildZip($zip_file, $files, $baseDir)
{
	if (file_exists($zip_file))
		unlink($zip_file);

	require_once 'Archive/Zip.php';
	$zip = new Archive_Zip($zip_file);
	$zip->add($files, array('remove_path' => $baseDir));
	if ($zip->errorCode() < 0)
		echo $zip->errorInfo();
}

function buildInstaller($zip_file, $xml_file)
{
	$baseDir = str_replace('\\', '/', dirname($xml_file)) . '/';
	$files = array($xml_file);
	$root = simplexml_load_file($xml_file);

	appendFiles($files, $root->files, 'filename', $baseDir);
	appendFiles($files, $root->media, 'filename', $baseDir);
	appendFiles($files, $root->languages, 'language', $baseDir);

	appendFiles($files, $root->administration->files, 'filename', $baseDir);
	appendFiles($files, $root->administration->media, 'filename', $baseDir);
	appendFiles($files, $root->administration->languages, 'language', $baseDir);

	buildZip($zip_file, $files, $baseDir);
}


?>
